package com.lzx.shop.controller;

import com.lzx.shop.dataobject.UserDO;
import com.lzx.shop.model.Result;
import com.lzx.shop.service.UserService;
import com.lzx.shop.utils.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName UserCntroller.java
 * @Description TODO
 * @createTime 2021年03月18日 00:30:00
 */

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String loginHtml(){
        return "login-new";
    }

    @PostMapping("/auth")
    public String loginUser(@RequestParam("userName")String userName, @RequestParam("pwd")String pwd, Model model, HttpSession session){
        Result result = new Result();
        result = userService.LoginByUserName(userName,pwd);
        if (result.isSuccess()){
            UserDO userDO = (UserDO) result.getData();
            session.setAttribute("userInfo",userDO);
            model.addAttribute("user",userDO);
            return "index";
        }
        model.addAttribute("error",result.getMsg());
        return "login-new";
    }



}
